Kotlin
List Specific Operations
Swift
|
Retrieving elements by index
|
val numbers = listOf(1, 2, 3, 4)
println(numbers.get(0))
println(numbers[0])
//numbers.get(5) // exception!
println(numbers.getOrNull(5)) // null
println(numbers.getOrElse(5, {it})) // 5
|
let numbers = [1, 2, 3, 4]
print(numbers.get(0))
print(numbers[0])
//numbers.get(5) // exception!
print(numbers.getOrNull(5)) // null
print(numbers.getOrElse(5, {it})) // 5
|
Retrieving list parts
|
val numbers = (0..13).toList()
println(numbers.subList(3, 6))
|
let numbers = (0..13).toList()
print(numbers.subList(3, 6))
|
Linear search
|
val numbers = listOf(1, 2, 3, 4, 2, 5)
println(numbers.indexOf(2))
println(numbers.lastIndexOf(2))
|
let numbers = listOf(1, 2, 3, 4, 2, 5)
print(numbers.indexOf(2))
print(numbers.lastIndexOf(2))
|
val numbers = mutableListOf(1, 2, 3, 4)
println(numbers.indexOfFirst { it > 2})
println(numbers.indexOfLast { it % 2 == 1})
|
let numbers = mutable[1, 2, 3, 4]
print(numbers.indexOfFirst { $0 > 2})
print(numbers.indexOfLast { $0 % 2 == 1})
|
Binary search in sorted lists
|
val numbers = mutableListOf("one", "two", "three", "four")
numbers.sort()
println(numbers)
println(numbers.binarySearch("two")) // 3
println(numbers.binarySearch("z")) // -5
println(numbers.binarySearch("two", 0, 2)) // -3
|
let numbers = mutableListOf("one", "two", "three", "four")
numbers.sort()
print(numbers)
print(numbers.binarySearch("two")) // 3
print(numbers.binarySearch("z")) // -5
print(numbers.binarySearch("two", 0, 2)) // -3
|
Comparator binary search
|
val productList = listOf(
Product("WebStorm", 49.0),
Product("AppCode", 99.0),
Product("DotTrace", 129.0),
Product("ReSharper", 149.0))
println(productList.binarySearch(Product("AppCode", 99.0), compareBy<Product> { it.price }.thenBy { it.name }))
|
let productList = listOf(
Product("WebStorm", 49.0),
Product("AppCode", 99.0),
Product("DotTrace", 129.0),
Product("ReSharper", 149.0))
print(productList.binarySearch(Product("AppCode", 99.0), compareBy<Product> { it.price }.thenBy { it.name }))
|
val colors = listOf("Blue", "green", "ORANGE", "Red", "yellow")
println(colors.binarySearch("RED", String.CASE_INSENSITIVE_ORDER)) // 3
|
let colors = listOf("Blue", "green", "ORANGE", "Red", "yellow")
print(colors.binarySearch("RED", String.CASE_INSENSITIVE_ORDER)) // 3
|
Comparison binary search
|
data class Product(val name: String, val price: Double)
fun priceComparison(product: Product, price: Double) = sign(product.price - price).toInt()
fun main() {
val productList = listOf(
Product("WebStorm", 49.0),
Product("AppCode", 99.0),
Product("DotTrace", 129.0),
Product("ReSharper", 149.0))
println(productList.binarySearch { priceComparison(it, 99.0) })
}
|
data class Product(let name: String, let price: Double)
func priceComparison(product: Product, price: Double) = sign(product.price - price).toInt()
func main() {
let productList = listOf(
Product("WebStorm", 49.0),
Product("AppCode", 99.0),
Product("DotTrace", 129.0),
Product("ReSharper", 149.0))
print(productList.binarySearch { priceComparison(it, 99.0) })
}
|
Adding
|
val numbers = mutableListOf("one", "five", "six")
numbers.add(1, "two")
numbers.addAll(2, listOf("three", "four"))
println(numbers)
|
let numbers = mutableListOf("one", "five", "six")
numbers.add(1, "two")
numbers.addAll(2, listOf("three", "four"))
print(numbers)
|
Updating
|
val numbers = mutableListOf("one", "five", "three")
numbers[1] = "two"
println(numbers)
|
let numbers = mutableListOf("one", "five", "three")
numbers[1] = "two"
print(numbers)
|
val numbers = mutableListOf(1, 2, 3, 4)
numbers.fill(3)
println(numbers)
|
let numbers = mutable[1, 2, 3, 4]
numbers.fill(3)
print(numbers)
|
Removing
|
val numbers = mutableListOf(1, 2, 3, 4, 3)
numbers.removeAt(1)
println(numbers)
|
let numbers = mutableListOf(1, 2, 3, 4, 3)
numbers.removeAt(1)
print(numbers)
|
val numbers = mutableListOf(1, 2, 3, 4, 3)
numbers.removeFirst()
numbers.removeLast()
println(numbers)
val empty = mutableListOf<Int>()
// empty.removeFirst() // NoSuchElementException: List is empty.
empty.removeFirstOrNull() //null
|
let numbers = mutableListOf(1, 2, 3, 4, 3)
numbers.removeFirst()
numbers.removeLast()
print(numbers)
let empty = mutableListOf<Int>()
// empty.removeFirst() // NoSuchElementException: List is empty.
empty.removeFirstOrNull() //null
|
Sorting
|
val numbers = mutableListOf("one", "two", "three", "four")
numbers.sort()
println("Sort into ascending: $numbers")
numbers.sortDescending()
println("Sort into descending: $numbers")
numbers.sortBy { it.length }
println("Sort into ascending by length: $numbers")
numbers.sortByDescending { it.last() }
println("Sort into descending by the last letter: $numbers")
numbers.sortWith(compareBy<String> { it.length }.thenBy { it })
println("Sort by Comparator: $numbers")
numbers.shuffle()
println("Shuffle: $numbers")
numbers.reverse()
println("Reverse: $numbers")
|
let numbers = mutableListOf("one", "two", "three", "four")
numbers.sort()
print("Sort into ascending: $numbers")
numbers.sortDescending()
print("Sort into descending: $numbers")
numbers.sortBy { it.length }
print("Sort into ascending by length: $numbers")
numbers.sortByDescending { it.last() }
print("Sort into descending by the last letter: $numbers")
numbers.sortWith(compareBy<String> { it.length }.thenBy { $0 })
print("Sort by Comparator: $numbers")
numbers.shuffle()
print("Shuffle: $numbers")
numbers.reverse()
print("Reverse: $numbers")
|